package com.xiye.common.util;

import java.math.BigInteger;

/**
 * @author 西野
 * @version V1.0
 * @title RightsHelper.java
 * @package com.xiye.common.util
 * @description 权限计算帮助类
 * @date 2018-07-11
 */
public class RightsHelper {

    /*
     * @Author 西野
     * @Date 2018-07-23 16:32:44
     * @Description 利用BigInteger对权限进行2的权的和计算
     * @Param [rights] int型权限编码数组
     * @Return java.math.BigInteger 2的权的和
     */
    public static BigInteger sumRights(int[] rights) {
        BigInteger num = new BigInteger("0");
        for (int i = 0; i < rights.length; i++) {
            num = num.setBit(rights[i]);
        }
        return num;
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:33:03
     * @Description 利用BigInteger对权限进行2的权的和计算
     * @Param [rights] String型权限编码数组
     * @Return java.math.BigInteger 2的权的和
     */
    public static BigInteger sumRights(String[] rights) {
        BigInteger num = new BigInteger("0");
        for (int i = 0; i < rights.length; i++) {
            num = num.setBit(Integer.parseInt(rights[i]));
        }
        return num;
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:33:23
     * @Description 测试是否具有指定编码的权限
     * @Param [sum, targetRights]
     * @Return boolean
     */
    public static boolean testRights(BigInteger sum, int targetRights) {
        return sum.testBit(targetRights);
    }

    /**
     * @Author 西野
     * @Date 2018-07-23 16:33:31
     * @Description 测试是否具有指定编码的权限
     * @Param [sum, targetRights]
     * @Return boolean
     */
    public static boolean testRights(String sum, int targetRights) {
        if (Tools.isEmpty(sum))
            return false;
        return testRights(new BigInteger(sum), targetRights);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:33:41
     * @Description 测试是否具有指定编码的权限
     * @Param [sum, targetRights]
     * @Return boolean
     */
    public static boolean testRights(String sum, String targetRights) {
        if (Tools.isEmpty(sum))
            return false;
        return testRights(new BigInteger(sum), targetRights);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:33:47
     * @Description 测试是否具有指定编码的权限
     * @Param [sum, targetRights]
     * @Return boolean
     */
    public static boolean testRights(BigInteger sum, String targetRights) {
        return testRights(sum, Integer.parseInt(targetRights));
    }
}
